﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TRAC_Demo
{
    // T.R.A.C.
    // Main Author: Ryan Redburn
    // Additional Authors:
    // Date: 10/5/2013

    #region [ Updates ]

    // Updated: 12/2/2013
    // By: Ryan Redburn
    // Updates: Bug fixes

    #endregion

    public partial class FormStandardManagement : Form
    {
        #region [ Fields ]

        private string errorTitle = "";
        private string errorMessage = "";

        #endregion

        #region [ Constructors ]

        public FormStandardManagement()
        {
            InitializeComponent();
            this.BackColor = CurrentUser.UserColor;
        }

        #endregion

        #region [ Events ]

        // Changes the color of the create form to provide an example to the user
        private void cmbColor_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string selection = (string)cmbColor.SelectedItem;
            List<int> colorValues = ColorKeeper.RetrieveColor(selection);
            this.BackColor = Color.FromArgb(colorValues[0], colorValues[1], colorValues[2], colorValues[3]);
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (ValidateChanges())
            {
                CommitChanges();
                MessageBox.Show("You have succesfully changed your user account.", "Changes Successful");
                this.Close();
            }
            else
            {
                MessageBox.Show(errorMessage, errorTitle);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion
        
        #region [ Methods ]

        // Commits user selected changes
        private void CommitChanges()
        {
            if (chkPassword.Checked && chkColor.Checked)
            {
                AccountKeeper.ChangePassword(CurrentUser.UserID, txtNewPassword.Text);
                AccountKeeper.ChangeUserColor(CurrentUser.UserID, (string)cmbColor.SelectedItem);
            }
            else if (chkPassword.Checked && !chkColor.Checked)
            {
                AccountKeeper.ChangePassword(CurrentUser.UserID, txtNewPassword.Text);
            }
            else if (!chkPassword.Checked && chkColor.Checked)
            {
                AccountKeeper.ChangeUserColor(CurrentUser.UserID, (string)cmbColor.SelectedItem);
            }
            else
            {
                errorTitle = "No Changes Selected";
                errorMessage = "You have not elected to make any changes.";
            }
        }

        // Validates user selection
        private bool ValidateChanges()
        {
            if (chkPassword.Checked && chkColor.Checked)
            {
                if (txtOldPassword.Text != "" && txtNewPassword.Text != "")
                {
                    if (cmbColor.SelectedItem != null)
                    {
                        if (AccountKeeper.ValidateUser(CurrentUser.UserID, txtOldPassword.Text))
                        {
                            return true;
                        }
                        else
                        {
                            errorTitle = "Invalid Password";
                            errorMessage = "The old password entered does not match your current password.";
                            return false;
                        }
                    }
                    else
                    {
                        errorTitle = "Invalid Color Selection";
                        errorMessage = "No color selection specified.";
                        return false;
                    }
                }
                else
                {
                    errorTitle = "Empty Fields";
                    errorMessage = "One or more required fields have been left empty.";
                    return false;
                }
            }
            else if (chkPassword.Checked && !chkColor.Checked)
            {
                if (txtOldPassword.Text != "" && txtNewPassword.Text != "")
                {
                    if (AccountKeeper.ValidateUser(CurrentUser.UserID, txtOldPassword.Text))
                    {
                        return true;
                    }
                    else
                    {
                        errorTitle = "Invalid Password";
                        errorMessage = "The old password entered does not match your current password.";
                        return false;
                    }
                }
                else
                {
                    errorTitle = "Empty Fields";
                    errorMessage = "One or more required fields have been left empty.";
                    return false;
                }
            }
            else if (!chkPassword.Checked && chkColor.Checked)
            {
                if (cmbColor.SelectedItem != null)
                {
                    return true;
                }
                else
                {
                    errorTitle = "Invalid Color Selection";
                    errorMessage = "No color selection specified.";
                    return false;
                }
            }
            else
            {
                errorTitle = "Select Command";
                errorMessage = "No command was selected.";
                return false;
            }
        }

        #endregion
    }
}
